package com.yanyun.yczyy.api.repository.impl;

import com.yanyun.yczyy.api.entities.ConfDynamicAlarm;
import com.yanyun.yczyy.api.repository.IConfDynamicAlarmRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class ConfDynamicRepository implements IConfDynamicAlarmRepository {
    @Override
    public List<ConfDynamicAlarm> all() {
        final String sql = "select * from conf_dynamic_alarm";
        try {
            return yczyy.query(sql, m_ConfDynamicAlarmMapper);
        } catch (Exception e) {
            log.error("查询动态配置失败:", e);
            return null;
        }
    }

    @Override
    public boolean upsert(ConfDynamicAlarm conf) {
        final String sql = "insert into conf_dynamic_alarm(id,name,value,type) values(?,?,?,?) on duplicate key update name = VALUES(name),value = VALUES(value),type = VALUES(type)";
        try {
            yczyy.update(sql, conf.getId(), conf.getName(), conf.getValue(), conf.getType());
            return true;
        } catch (Exception e) {
            log.error("更新动态配置失败:", e);
            return false;
        }
    }

    private final RowMapper<ConfDynamicAlarm> m_ConfDynamicAlarmMapper = new BeanPropertyRowMapper<>(ConfDynamicAlarm.class);
    @Autowired
    JdbcTemplate yczyy;

}
